<?php

namespace App\Repository\Logs;

use App\Models\LogsModel as Model;
use App\Repository\BaseRepository;

class LogsRepository extends BaseRepository
{
    public $model;
    public $log;

    public function __construct()
    {
        $this->model = new Model();
//    $this->log = new Logger('anchor');
//    $this->log->pushHandler(
//      (new RotatingFileHandler(storage_path('/logs/anchor.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
//    );
        parent::__construct();
    }


//日志列表
    public function dataList($type, $tableId, $page = 1, $limit = 10)
    {
        $pk = $this->model->getKeyName();
//        先要总数
        $where = ['type' => $type, 'table_id' => $tableId];
        $totalRows = $this->model->where($where)->count($pk);
        $rows = $this->model->with(["user" => function ($query) {
            $query->select(['id', 'nick_name']);
        }])->where($where)
            ->page($page, $limit)
            ->select(['users_id', 'created_at', 'content'])->orderByDesc('id')->get();

        return $this->returnMsg(200, '操作成功', ['dataList' => $rows], $totalRows);
    }

//保存记录
    public function add($type, $userId, $tableId, $content)
    {
        $this->model->saveIt($type, $userId, $tableId, $content);
    }

}
